package com.zy.hospitalward_management_system.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.hospitalward_management_system.pojo.Bed;
import com.zy.hospitalward_management_system.pojo.Inpatientward;
import com.zy.hospitalward_management_system.service.impl.BedServiceImpl;
import com.zy.hospitalward_management_system.service.impl.DepartmentServiceImpl;
import com.zy.hospitalward_management_system.service.impl.InpatientwardServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zy
 * @since 2021-01-26
 */
@Controller
public class InpatientwardController {

    @Resource
    InpatientwardServiceImpl inpatientwardService;

    @Resource
    DepartmentServiceImpl departmentService;

    @Resource
    BedServiceImpl bedService;


    /**
     * 根据科室ID 查询所有所属该科室的病房
     * @param DepartmentId 科室ID
     * @return JSON格式的病房列表
     */
    @ResponseBody
    @RequestMapping("/inpatientSelect")
    public String inpatientSelect(int DepartmentId){
        List<Inpatientward> inpatientwards = inpatientwardService.queryInpatientwardsByDepartmentId(DepartmentId);
        return JSON.toJSONString(inpatientwards);
    }

    @GetMapping("/selectInpatientWardsManageInfoByDepartmentId/{id}")
    public String selectInpatientWardsManageInfoByDepartmentId(@PathVariable("id") String departmentId, Model model){
        List<Map<String, String>> departmentKinds = departmentService.getBaseMapper().selectDepartmentKinds();
        model.addAttribute("departmentKinds",departmentKinds);
        List<Map<String, String>> infoMaps = inpatientwardService.getBaseMapper().selectInpatientWardsManageInfoByDepartmentId(Integer.parseInt(departmentId));
        model.addAttribute("infoMaps",infoMaps);
        model.addAttribute("departmentId",Integer.parseInt(departmentId));
        return "user/list/inpatientward_list";
    }


    /**
     * ajax请求接口 传入病房对象 添加病房.
     * @param inpatientward 病房对象
     * @return 返回信息
     */
    @PostMapping("/inpatientward/add")
    @ResponseBody
    public String addInpatientWard(Inpatientward inpatientward){
        System.out.println(inpatientward);
        if(inpatientward.getInpatientwardId()==null){
            return "请输入病房号!";
        }
        if(inpatientward.getInpatientwardType().trim().isEmpty()){
            return "请输入病房类型!";
        }
        if(inpatientward.getInpatientwardCost()==null){
            return "请输入病房价格!";
        }

        Inpatientward existsInpatientward = inpatientwardService.getBaseMapper().selectById(inpatientward.getInpatientwardId());
        if(existsInpatientward!=null){
            return "该病房号已经被使用!";
        }

        int insert = inpatientwardService.getBaseMapper().insert(inpatientward);
        if(insert==1){
            return "添加成功!";
        }else {
            return "添加失败!";
        }

    }

    /**
     * 删除病房
     * @param inpatientWardId
     * @return
     */
    @GetMapping("/inpatientward/delete")
    @ResponseBody
    public String deleteInpatientwardById(String inpatientWardId){
        System.out.println("test======="+inpatientWardId);

        //先判断一下床上有没有人.
        //查询病床表里面,所属病房为形参inpatientWardId,并且bed_state为1的count
        Bed bed = bedService.getBaseMapper().selectOne(new QueryWrapper<Bed>().eq("inpatientward_id", Integer.parseInt(inpatientWardId)).eq("bed_state", 1));
        if(bed!=null){
            return "请先办理"+bed.getBedId()+"号床病人的出院手续!";
        }


        //病房表根据ID删除病房
        int i = inpatientwardService.getBaseMapper().deleteById(Integer.parseInt(inpatientWardId));
        if(i==1){
            //病床表删除所有属于该病房ID的病床
            bedService.getBaseMapper().delete(new QueryWrapper<Bed>().eq("inpatientward_id", inpatientWardId));
            return "删除成功!";
        }

        return "删除失败!";
    }


    @GetMapping("/inpatientward/search")
    @ResponseBody
    public String queryInpatientWardById(String id,Model model){
        if(id.isEmpty()){
            return "请输入房间号!";
        }
        List<Map<String, String>> bedInfos = bedService.getBaseMapper().queryBedInfoByInpatientWardId(Integer.parseInt(id));

        if(bedInfos.isEmpty()){
            return "未查询到房间!";
        }
        model.addAttribute("inpatientWardId",id);
        model.addAttribute("bedInfos",bedInfos);
        return "success";
        }

}

